package com.example.springbootdemo.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.springbootdemo.bean.Result;
import com.example.springbootdemo.entity.User;
import com.example.springbootdemo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 廖老师
 * @since 2023-11-24
 */
@Controller
@RequestMapping("/springbootdemo/user")
public class UserController {

    @Resource
    UserMapper mapper;

    @RequestMapping("allUser")
    public String thymeleaf(Model model){
        final List<User> users = mapper.selectList(null);
        model.addAttribute("users", users);
        return "thymeleaf";
    }

    @RequestMapping("sendEmail")
    @ResponseBody
    public Result sendEmail(String name, HttpSession session){
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getName, name);
        final User user = mapper.selectOne(lqw);
        if(user == null){
            return new Result(0, "用户名不正确!",null);
        }
        // 发送邮件
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        String content = "请求点击<a href='http://127.0.0.1:8080/springbootdemo/user/resetPwd/"+uuid+"'>重置</a>链接,完成修改密码!";
        String title = "重置密码";
        session.setAttribute("resetPwdUuid", uuid);
        sendHtmlMail(user.getEmail(), title, content);
        return new Result(1,"邮件发送成功!",null);
    }

    @RequestMapping("resetPwd/{uuid}")
    public String resetPwd(@PathVariable String uuid, @SessionAttribute String resetPwdUuid){
        if(uuid.equals(resetPwdUuid)){
            //return "forward:resetPwd";
            return "resetPwd";
        } else {
            return "redirect:/忘记密码.html";
        }
    }


    @Resource
    JavaMailSender mailSender;
    @Value("${mail.fromMail.addr}")
    private String from;

    public void sendHtmlMail(String to, String subject, String content) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            //true表示需要创建一个multipart message
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(from);
            helper.setSubject(subject);
            helper.setText(content, true);
            mailSender.send(message);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }



}

